import inputCommon from '../../../../mixin/inputCommon'
import { invokeInterface, getDictionaryList } from '../../../../service'
import requiredLabel from '../../../../components/requiredLabel'

export default {
  name: 'checkboxRender',
  components: { requiredLabel },
  mixins: [inputCommon],
  props: ['config', 'editor'],
  inject: ['rootForm'],
  data() {
    return {
      default: [],
      options: [{ label: '复选1', value: '复选1a' }, { label: '复选2', value: '复选2a' }]
    }
  },
  methods: {
    // change(value) {
    //   this.$refs.formItem.$emit('el.form.change', value)
    //   return this.config.changeCb && this.getParsedFn(.changeCb')()
    // }
  },
  created() {
    if (this.config.connectType === 'dictionary' && this.config.dataDictionaryId) {
      this.options = []
      getDictionaryList(this.config.dataDictionaryId).then(data => {
        this.options = data.map(one => ({
          label: one.label,
          value: one.value || one.label
        }))
      })
    } else if (this.config.connectType === 'interface' && this.config.dataInterfaceId) {
      // 关联查询
      if (window.sessionStorage.formConfig) {
        let oldConfig = JSON.parse(window.sessionStorage.formConfig)

        if (oldConfig.dataInterfaceId === this.config.dataInterfaceId && this.config.searchClick === oldConfig.searchClick) {
          return
        }else{
          window.sessionStorage.setItem('formConfig', JSON.stringify(this.config))
        }
      }else{
        window.sessionStorage.setItem('formConfig', JSON.stringify(this.config))
      }
      let params = {}
      // let linkSearchExFn = this.getParsedEx('linkSearchEx')
      // if (this.config.linkSearch) {
      //   let v = linkSearchExFn()
      //   if (v !== 0) {
      //     v = v || ''
      //   }
      //   params[this.config.linkSearchQueryKey] = v
      // }
      this.options = []
      invokeInterface(this.config.dataInterfaceId, params).then((data) => {
        this.options = data.map(one => ({
          label: one[this.config.optionsLabelKey],
          value: one[this.config.optionsValueKey]
        }))
      })
    }
  }
}
